package sbc.ms;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.xvsm.api.core.Capi;
import org.xvsm.api.core.ContainerRef;
import org.xvsm.api.core.CoordinationTypes;
import org.xvsm.api.core.notification.NotificationListener;
import org.xvsm.api.core.notification.NotificationMode;
import org.xvsm.api.core.notification.NotificationTarget;
import org.xvsm.client.ClientCapi;

public class Consumer extends Thread {
	private Capi capi;
	private NotificationListener listener;

	public Consumer(String url, NotificationListener listener) {
		this.listener = listener;
		try {
			Logger.getLogger("org.xvsm.client").setLevel(Level.WARN);
			this.capi = new ClientCapi(url);
			this.capi.init(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {

		try {
			ContainerRef cref = Util.getOrCreateNamedContainer(capi,
					"Container", CoordinationTypes.FIFO);
			System.out.println("CREF: " + cref);

			capi.createNotification(cref, -1, NotificationTarget.ANY.code(),
					NotificationMode.INFINITE, true,
					listener);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
